# -*- coding: UTF-8 -*-
import os
import os.path
import sys
current_dir = os.path.abspath(os.path.dirname(__file__))
sys.path.append(current_dir)
sys.path.append("..")
from file_manage import models
from Method_Decorate import Get_django_log
import logging.config

import ruamel.yaml as yaml

import CaseCollection
from file_method import Location_Sample

# if __name__ == '__main__':
#string_id为进度条查询事项
def Auto_process(temp_dic,pk=None):
    print (os.getcwd())
    logging.config.fileConfig('./Auto_Code/logging.conf')
    # setting loading
    # with open("./setting/main_set.yaml", 'r+') as f:
    #     temp_dic = yaml.load(f.read(), Loader=yaml.Loader)
    # #
    ttt = CaseCollection.CaseCollection()
    fail_num=-1
    try:
        total_case_path_temp=Location_Sample.win_to_ubuntu_filepath(temp_dic['Total_case_path'],'Src_Code')
        total_case_path_temp.encode("UTF-8")
        ttt.loadFromYaml(total_case_path_temp)
        app_path_temp=Location_Sample.win_to_ubuntu_filepath(temp_dic['app_path'],'Src_Code')
        #获取app状态 1.9 wwm
        app_database=models.App.objects.get(app_path=temp_dic['app_path'])
        if app_database.judge_condition_compile():
            raise Exception("正在编译！不能使用")
        app_database.set_condition_run()

        report_path_temp=Location_Sample.win_to_ubuntu_filepath(temp_dic['report_path'],'Src_Code')
        #如果report_path_temp不存在时
        if not os.path.exists(report_path_temp):
            os.makedirs(report_path_temp)
        fail_num=ttt.run(app_path_temp,  report_path_temp,pk)

    except Exception as e:
        Get_django_log.log_action.log_django.error(e)
    finally:
        print('错误的数据有{0}'.format(fail_num))
        app_database = models.App.objects.get(app_path=temp_dic['app_path'])
        app_database.set_condition_free()
        return fail_num

   
# def test1():
#
#     print "Just a test"
#     #PythonTest.test()
#     #c1 = cash.cash(1)
#     #c1.setFileAttribute("ADC0FileName","c:\\test\\adc0.raw")
#     #c1.setCashAttribute("value",100)
#     #c2 = cash.cash(2)
#     #print c2
#
#     #test_case = case.case()
#     #test_case.newBundle()
#     #test_case.addBundleSetup(["set --value_mode count"])
#     #test_case.addBundleTearoff(["set --value_mode single"])
#     #test_case.addCash(c1)
#     #test_case.addCash(c2)
#     #test_case.setDescriptionAttribute("name", "the first test case")
#
#     #print c2
#     #outfile = open("test.yaml", 'w')
#     #test_case.dumpToYaml(outfile)
#     #outfile.close()
#
#     #print test_case.getStandardCash(0,1)
#
#     #print test_case
#
#
#     case2 = case.case()
#     try:
#         with open("sample.yaml", 'r') as infile:
#             case2.loadFromYaml(infile)
#     except IOError, e:
#         print e
#         print "open file %s failed\n"%"sample.yaml"
#     except:
#         print "something wrong \n"
#     finally:
#         pass
#         #print "something wrong\n"
#
#     #print case2
#
#
#     #print test_case
#
#     #genYAML.genYaml("","E:\\test")
#
#     case2.run("D:\\work\\cashtest\\x64\\Debug\\app.exe")
#
#     case2.genCaseReport("test.html")



   




    
